<?php
require __DIR__ . '/../../bootstrap.php';
require_once __DIR__ . "/../../auth.php";
require_once __DIR__ . "/../functions.php";
$em = require __DIR__ . '/../../example/em.php';
$auth = new \Auth\Auth($em);
$user = $auth->getUser();


$dateBegin = '2011-01-01';
if (isset($_GET['dateBegin'])) {
    $dateBegin = convertDate($_GET['dateBegin']);
}

$dateEnd = date("Y-m-d");
if (isset($_GET['dateEnd'])) {
    $dateEnd = convertDate($_GET['dateEnd']);
}


/** @var $qb \Doctrine\ORM\QueryBuilder */
$qb = $em->createQueryBuilder();
$qb->select(array('u.name', 'so.date', 's.number'))
    ->from("Entity\User", 'u')
    ->join("u.sims", "so")
    ->join("so.sim", "s")
    ->where("so.state=1")

    ->andWhere("so.date between '$dateBegin 00:00:00' and '$dateEnd 23:59:59' ")

    ->andWhere($qb->expr()->gte('u.lft', ':ltf'))
    ->andWhere($qb->expr()->lte('u.rgt', ':rgt'))
    ->setParameter("ltf", $user->getLeft())
    ->setParameter("rgt", $user->getRight());

$rows = $qb->getQuery()->getResult();
$result["aaData"]=array();
$i = 0;
foreach ($rows as $row) {
    $i++;
    $result["aaData"][] = array($i, $row['number'], $row['name'], $row['date']->format('d.m.Y'));
}
echo json_encode($result);
?>
